export default {
  install (Vue) {
    /**
     * loading: true | false
     * @param obj
     */
    Vue.prototype.$tools = {
      loading: function (bool) {
        var ys = bool ? 'style="display:block"' : 'style="display:none"'
        var loading = '<div class="loader-inner ball-clip-rotate" ' + ys + '><div></div><span></span></div>'
        if (bool) {
          document.getElementById('l-loadingcont').innerHTML = loading
        } else {
          document.getElementById('l-loadingcont').innerHTML = ''
        }
      },
      /**
       * 支付post提交方式
       * @param URL PARAMS TYPE : 1 || '' post 2 get
       */
      payWay: function (URL, PARAMS, TYPE) {
        var temp = document.createElement('form')
        temp.action = URL
        temp.method = TYPE === 1 || '' ? 'post' : 'get'
        temp.style.display = 'none'
        for (var x in PARAMS) {
          var opt = document.createElement('textarea')
          opt.name = x
          opt.value = PARAMS[x]
          temp.appendChild(opt)
        }
        document.body.appendChild(temp)
        temp.submit()
        return temp
      },
      /**
       * lookmore: bool, booltype, dom
       * @param bool 是否显示整个; booltype 显示具体哪个 true 没有更多 false 加载中; dom 追加到哪个对象
       */
      lookmore: function (bool, booltype) {
        if (document.getElementById('l-more')) {
          var ys = booltype ? 'style="display:block"' : 'style="display:none"'
          var ys2 = booltype ? 'style="display:none"' : 'style="display:block"'
          var more = '<div class="no-more" ' + ys + '>没有更多商品了</div><div class="ball-pulse" ' + ys2 + '>加载中<div></div><div></div><div></div></div>'
          if (bool) {
            document.getElementById('l-more').innerHTML = more
          } else {
            document.getElementById('l-more').innerHTML = ''
          }
        }
      },
      // 验证手机号
      isPhone: function (phone) {
        var re
        re = /1[3456879]\d{9}$/
        if (re.test(phone)) {
          return true
        } else {
          return false
        }
      },
      getFormt: function (type) {
        let nowDate = new Date()
        let year = nowDate.getFullYear()
        let month = nowDate.getMonth() + 1 < 10 ? '0' + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1
        let day = nowDate.getDate() < 10 ? '0' + nowDate.getDate() : nowDate.getDate()
        let str = ''
        switch (type) {
          case 1 :
            str = year + '-' + month + '-' + day
            break
          default:
        }
        return str
      }
    }
  }
}
